package function.listen;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;

import javax.swing.JPanel;

import frame.closefileconfirm;
import frame.master;
import frame.data.contrldata;
/**
 * 文本标签
 * @author Administrator
 *
 */
public class filetablisten {
	private Vector<Integer> tabwidth=new Vector<>();//记录每一个文件tab宽度
	public filetablisten() {
		contrldata.filetab=new JPanel() {
			@Override
			protected void paintComponent(Graphics g) {
				drawfiletab(g,getWidth(),getHeight());
			}
		};
		contrldata.filetab.setPreferredSize(new Dimension(master.WIDTH, 30));
	}
	/**
	 * 加载标签监听
	 */
	public void loadfiletablisten(master ma) {
		contrldata.filetab.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}
			@Override
			public void mouseEntered(MouseEvent e) {}
			@Override
			public void mouseClicked(MouseEvent e) {
				int x=e.getX(),y=e.getY(),ww=0;
				for(int i=0;i<tabwidth.size();i++) {
					if(x>ww && x<ww+tabwidth.get(i)) {
						if(x>ww+tabwidth.get(i)-15 && x<ww+tabwidth.get(i)-4 && y>10 && y<20) {
							if(master.fl.isfileupdateofnum(i)) {
								new closefileconfirm(master.fl.getfileinfoofnum(i),master.fl.getfileoldurlofnum(i),i,ma);
							}else {
								master.fl.deletefileofnum(i,ma);
							}
						}else {
							master.editfilenum=i;
							contrldata.filetab.updateUI();
							contrldata.jt.setText("");
							HashMap<Integer, String> a=master.fl.getfileinfoofnum(master.editfilenum).getContent();
							Set<Integer> s=a.keySet();
							for(int ii:s)contrldata.jt.append(a.get(ii)+"\n");
							master.t2l.updatelinenum(ma);
						}
					}
					ww+=tabwidth.get(i);
				}
			}
		});
	}
	/**
	 * 绘制标签
	 * @param g
	 * @param width
	 * @param height
	 */
	private void drawfiletab(Graphics g,int width,int height) {
		g.setColor(new Color(240,240,240));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("黑体",0,15));
		FontMetrics fm=g.getFontMetrics();
		int leftx=0,w=0;
		tabwidth.clear();
		for(int i=0;i<master.fl.getfilenum();i++) {
			w=30+fm.stringWidth(master.fl.getfilenameofnum(i));
			tabwidth.add(w);
			if(i==master.editfilenum)g.setColor(Color.white);
			else g.setColor(new Color(235,235,235));
			g.fillRect(leftx, 0,w, 30);
			g.setColor(new Color(209,212,219));
			g.drawLine(leftx, 0, leftx, 30);
			g.drawLine(leftx, 0, leftx+w, 0);
			g.drawLine(leftx+w, 0, leftx+w, 30);
			g.setColor(Color.black);
			g.drawString(master.fl.getfilenameofnum(i), leftx+4, 20);
			g.drawLine(leftx+w-15, 10, leftx+w-4, 20);
			g.drawLine(leftx+w-15, 20, leftx+w-4, 10);
			leftx+=w;
		}
	}
}
